package com.test.liuhb.test;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.GestureDetector;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.android.print.sdk.PrinterConstants;
import com.android.print.sdk.PrinterInstance;

import com.test.liuhb.test.Gson.GsonTest_;
import com.test.liuhb.test.Util.BluetoothOperation;
import com.test.liuhb.test.Util.IPrinterOpertion;
import com.test.liuhb.test.hktable.DataTable;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends Activity implements View.OnClickListener{
    ProgressBar pb;
    private Timer timer=null;
    private TimerTask task=null;
    private int progress=0;
    String BuleToothAddress;
    TextView tv;
    EditText barcode;
    SeekBar seekBar,seekBar1;
    private PrinterInstance mPrinter;
    public IPrinterOpertion myOpertion;
    GridView gridView;
    private ArrayAdapter<String> adapter;
    Toast toast;
    // 手势识别器
    private GestureDetector mDetector;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pb=(ProgressBar) findViewById(R.id.progressBar2);
        tv=(TextView)findViewById(R.id.tv);
        seekBar=(SeekBar)findViewById(R.id.seekBar1);
        barcode=(EditText)findViewById(R.id.barcode);
        seekBar1=(SeekBar)findViewById(R.id.seekBar2);
        gridView=(GridView)findViewById(R.id.gridView);
        barcode.requestFocus();
        pb.setMax(100);
        pb.setProgress(50);
        seekBar1.setMax(100);
        adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1);
        gridView.setAdapter(adapter);
        for(int i=0;i<10;i++){
            adapter.add("网格"+i);
        }
        seekBar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                tv.setText(String.format("当前进度为：%d",i)+"%");
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
       /* mDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {

            *//**
             * 监听手势滑动事件:e1表示滑动的起点，e2表示滑动终点， velocityX表示水平速度， velocityY表示垂直速度
             *//*
            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2,
                                   float velocityX, float velocityY) {

                // 判断纵向滑动是否过大，过大的话不允许切换界面
                if (Math.abs(e2.getRawY() - e1.getRawY()) > 200) {
                    Toast.makeText(MainActivity.this, "不能这样划哦",
                            Toast.LENGTH_SHORT).show();
                    return true;
                }

                // 判断滑动是否龟速，如果速度过慢也不允许滑动到另一页
                if (Math.abs(velocityX) < 100) {
                    Toast.makeText(MainActivity.this, "滑动太龟速哦",
                            Toast.LENGTH_SHORT).show();
                    return true;
                }

                // 向右滑动,上一页
                if (e2.getRawX() - e1.getRawX() > 200) {
                    showPreviousPage();
                    return true;
                }
                // 向左滑动,下一页
                if (e1.getRawX() - e2.getRawX() > 200) {
                    showNextPage();
                    return true;
                }

                return super.onFling(e1, e2, velocityX, velocityY);
            }
        });*/
    }


    /*private void showPreviousPage() {
        Toast.makeText(this, "已经是第一页罗，不能再滑拉", Toast.LENGTH_SHORT).show();
    }

    private void showNextPage() {
        // 在这里跳到第二个页面
        Intent intent = new Intent(this, ImageActivity.class);
        startActivity(intent);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // 委托手势识别器处理触摸事件
        mDetector.onTouchEvent(event);
        return super.onTouchEvent(event);

    }
*/

    @Override
    protected void onResume() {
        super.onResume();
        startTimer();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        stopTimer();
    }

    public void startTimer(){
    if(timer==null){
       timer=new Timer();
        task=new TimerTask() {
            @Override
            public void run() {
                progress++;
                pb.setProgress(progress);
            }
        };
        timer.schedule(task,1000,1000);
    }
    }
    public void stopTimer(){
        if(timer!=null){
          timer.cancel();
            task.cancel();
            task=null;
            timer=null;
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn1:
                Intent i=new Intent(MainActivity.this,TestActivity.class);
                startActivity(i);

                break;
            case R.id.btn2:
                Intent i1=new Intent(MainActivity.this,Test1Activity.class);
                startActivity(i1);

                break;
            case R.id.btn3:
                toast = Toast.makeText(this, "RecyclerViewActivity", Toast.LENGTH_SHORT);
                toast.show();
                Intent i11=new Intent(MainActivity.this,RecyclerViewActivity.class);
                startActivity(i11);
                break;
            case R.id.btn4:
                toast = Toast.makeText(this, "通知", Toast.LENGTH_SHORT);
                toast.show();
                Intent i12=new Intent(MainActivity.this,NotificationTestActivity.class);
                startActivity(i12);
                break;
            case R.id.btn5:
                toast = Toast.makeText(this, "androidannotations", Toast.LENGTH_SHORT);
                toast.show();
                Intent i13=new Intent(MainActivity.this,AAFrameActivity_.class);
                startActivity(i13);
                break;
            case R.id.btn6:
                toast = Toast.makeText(this, "Gson", Toast.LENGTH_SHORT);
                toast.show();
                Intent i14=new Intent(MainActivity.this,GsonTest_.class);
                startActivity(i14);
                break;
            case R.id.btn7:
                toast = Toast.makeText(this, "PopupWindow", Toast.LENGTH_SHORT);
                toast.show();
                Intent i15=new Intent(MainActivity.this,GsonTest_.class);
                startActivity(i15);
                break;
            case R.id.btn8:
                myOpertion = new BluetoothOperation(this, mHandler);
                myOpertion.chooseDevice();
                toast = Toast.makeText(this, "打印条码", Toast.LENGTH_SHORT);
                toast.show();
                break;
            case R.id.btn9:
                doPrint(barcode.getText().toString());
                break;
            default:
                break;
        }
    }
    public void doPrint(String barcode){
        try {
            PrinterUitl.printBarCode(barcode,
                   myOpertion.getPrinter());

        } catch (Exception e) {
//            playError();
//            HKDialog1 dailog = new HKDialog1(MakeSmallTicketActivity.this, getResStr(R.string.m9_POS_Btn_printErr));
//            dailog.show();

        }
    }
    //用于接收连接状态的handler
    private final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case PrinterConstants.Connect.SUCCESS:
                    mPrinter = myOpertion.getPrinter();
//                    m9_StoerCheck_print.setText(getResStr(R.string.m8_printlabel_toast_connect_tip_2));
//                    toast(getResStr(R.string.m8_printlabel_toast_connect_tip_1));
                    break;
                case PrinterConstants.Connect.FAILED:
                    //toast(getResStr(R.string.m8_printlabel_toast_connect_tip_3));
                    break;
                case PrinterConstants.Connect.CLOSED:
                    //toast(getResStr(R.string.m8_printlabel_toast_connect_tip_4));
                    break;
                case PrinterConstants.Connect.NODEVICE:
                    //toast(getResStr(R.string.m8_printlabel_toast_connect_tip_5));
                    break;
                default:
                    break;
            }
        }
    };
    @Override
    protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case 1:
                BuleToothAddress = data.getStringExtra("device_address");

                System.out.println(BuleToothAddress);

                //因为连接蓝牙时会出现几秒的卡顿,所以采用异步连接
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        myOpertion.open(data);
                    }
                }).start();
                break;
        }
    }
}
